<template>
    <div class="shop">
        <div class="header">
            <span>店铺管理</span>
            <el-button type="primary" @click="save">保存</el-button>
        </div>
        <el-form ref="form" :model="form" label-width="80px" style="width: 60%">
            <el-form-item label="店铺名称">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="店铺公告">
                <el-input type="textarea" v-model="form.bulletin"></el-input>
            </el-form-item>
            <el-form-item label="店铺头像">
                <el-upload class="avatar-uploader" action="http://sell.h5.itsource.cn:8087/shop/upload"
                    :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item label="店铺图片">
                <el-upload action="http://sell.h5.itsource.cn:8087/shop/upload" list-type="picture-card"
                    :on-preview="preview" :on-remove="remove" :file-list="fileList">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

            <el-form-item label="配送费">
                <el-input v-model="form.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送时间">
                <el-input v-model="form.deliveryTime"></el-input>
            </el-form-item>
            <el-form-item label="配送描述">
                <el-input v-model="form.description"></el-input>
            </el-form-item>
            <el-form-item label="店铺评分">
                <el-input v-model="form.score"></el-input>
            </el-form-item>
            <el-form-item label="销量">
                <el-input v-model="form.sellCount"></el-input>
            </el-form-item>
            <el-form-item label="活动">
                <el-checkbox-group v-model="form.supports">
                    <el-checkbox label="在线支付满28减5" name="supports"></el-checkbox>
                    <el-checkbox label="vc无限橙果汁全场8折" name="supports"></el-checkbox>
                    <el-checkbox label="单人精彩套餐" name="supports"></el-checkbox>
                    <el-checkbox label="特价饮品8折抢购" name="supports"></el-checkbox>
                    <el-checkbox label="单人特色套餐" name="supports"></el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="营业时间">
                <el-time-picker is-range v-model="value1" range-separator="至" start-placeholder="开始时间"
                    end-placeholder="结束时间" placeholder="选择时间范围">
                </el-time-picker>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { shopInfo } from '@/api/shop.js'
export default {
    async created () {
        let res = await shopInfo()
        this.form = res.data.data
        this.fileList = res.data.data.pics.map(v => {
            return { url: v }
        })
        this.imageUrl = res.data.data.avatar
    },
    data () {
        return {
            //表单数据
            form: {
                supports: [],
            },
            //图片墙
            dialogImageUrl: '',
            dialogVisible: false,
            fileList: [],
            //头像
            imageUrl: '',
            //营业时间
            value1: [new Date(2016, 9, 10, 8, 40), new Date(2016, 9, 10, 9, 40)],

        }

    },
    methods: {
        save () {
            this.form.pics = this.fileList.map(v => {
                return v.url
            })
            console.log(this.fileList);
        },
        //头像上传成功之后
        handleAvatarSuccess (res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        //头像上传成功之前
        beforeAvatarUpload (file) {
            let mime = ['image/jpeg', 'image/png', 'image/gif']
            const isJPG = mime.includes(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG PNG GIF 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },

        //删除图片墙
        remove (file, fileList) {
            console.log(file, fileList);
        },
        //预览图片墙
        preview (file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        }
    }
}
</script>
<style lang="less" scoped>
.header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
}

:deep(.avatar-uploader .el-upload) {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>